<?
include('aux_includes.php');

session_start();
if ($_GET['restart'] == 'true') {
	session_destroy();
	session_start();
}

$racas[] = new Gigante('1');
$racas[] = new Minotauro('1');
$racas[] = new Anao('1');
$racas[] = new Humano('1');
$racas[] = new Elfo('1');
$racas[] = new Driade('1');
$racas[] = new Vampiro('1');
$racas[] = new Orque('1');
$racas[] = new MortoVivo('1');

echo '<table border="1"><tr><td></td>';
for ($j=0; $j<sizeof($racas); $j++) {
	$raca = $racas[$j];
	echo '<td>' . $raca->getValue('nome') . '</td>';
}

for ($i=0; $i<sizeof($racas); $i++) {
	echo '<tr>';
	$raca = $racas[$i];
	echo '<td>' . $raca->getValue('nome') . '</td>';
	for ($j=0; $j<sizeof($racas); $j++) {
		echo '<td>';
		
		if ($racas[$i] != $racas[$j]) {
			
			$raca1 = new $racas[$i]('1');
			$raca2 = new $racas[$j]('1');
			
			$jog1_vit=0; $jog2_vit=0;
			for ($k=1; $k<=10; $k++) {
				
				$jog1 = new Actor($raca1->getValue('nome'), $raca1, 'm'); $_SESSION['jog1'] = $jog1;
				$jog2 = new Actor($raca2->getValue('nome'), $raca2, 'm'); $_SESSION['jog2'] = $jog2;

				//START OF COMBAT
				// initiative check
				$jog1_initiative = $jog1->getValue('initiative') + rand(1, 20);
				$jog2_initiative = $jog2->getValue('initiative') + rand(1, 20);

				if ($jog1_initiative > $jog2_initiative) $_SESSION['ultimo_turno'] = 1;
				else $_SESSION['ultimo_turno'] = 2;

				$continue = true;
				while($continue) {
					
					$_SESSION['rounds']++;
					if ($_SESSION['ultimo_turno'] == 1) {
						$atacante = $_SESSION['jog1']; $defensor = $_SESSION['jog2'];
					} else {
						$atacante = $_SESSION['jog2']; $defensor = $_SESSION['jog1'];
					}
					
					// GET ATTACKER MODIFIERS
					$att_size_mod = floor((6 - $atacante->getValue('size')) * 1.2);
					$att_str_mod = getMod($atacante->getValue('str'));
					$att_dex_mod = getMod($atacante->getValue('dex'));
					$att_con_mod = getMod($atacante->getValue('con'));
					$att_int_mod = getMod($atacante->getValue('int'));
					$att_wis_mod = getMod($atacante->getValue('wis'));
					$att_car_mod = getMod($atacante->getValue('car'));
					
					// GET DEFENDER MODIFIERS
					$def_size_mod = floor((6 - $defensor->getValue('size')) * 1.2);
					$def_str_mod = getMod($defensor->getValue('str'));
					$def_dex_mod = getMod($defensor->getValue('dex'));
					$def_con_mod = getMod($defensor->getValue('con'));
					$def_int_mod = getMod($defensor->getValue('int'));
					$def_wis_mod = getMod($defensor->getValue('wis'));
					$def_car_mod = getMod($defensor->getValue('car'));
					
					// ATTACK ROLL
					$dado = new Dado(20);
					$attack_roll = $dado->lancar(1, 0);

					// ARMOR CLASS
					$armor_class = 10 + $defensor->getValue('naturalArmor') + $armor_bonus + $shield_bonus + $def_dex_mod + 	$def_size_mod;
						
					$action = 'attack_melee';
					if ($action == 'attack_ranged' && !$ranged_attack_allowed) $action = 'failed';

					if ($action!='failed') {
						if ($attack_roll > 1) {
							if ($action == 'attack_melee') {
								$armor_class += $armor_on_melee_mod;
								$attack_roll += $melee_attack_mod;
								$attack_bonus = $atacante->getValue('baseAttack') + $att_str_mod + $att_size_mod;
							}
							if ($action == 'attack_ranged') {
								$armor_class += $armor_on_ranged_mod;
								$attack_roll += $ranged_attack_mod;
								$attack_bonus = $atacante->getValue('baseAttack') + $att_dex_mod + $att_size_mod;
							}
						} else $attack_bonus = 0;
						
						// GET THE RESULT!
						$attack_roll += $attack_bonus;
						$resultado = $attack_roll - $armor_class;
						
						if ($resultado > 0) {
							// DAMAGE ROLL
							$damage_roll = $atacante->getArma()->damage();
							if ($action == 'attack_melee')
								$dano_final = $damage_roll + $att_str_mod;
							
							// CRITICAL HIT
							$critical = $atacante->getArma()->getValue('critical');
							if ($damage_roll >= 20 - $critical[1]) $dano_final *= $critical[0];
							
							// DAMAGE IS AT LEAST 1
							if ($dano_final <= 0) $dano_final = 1;
								
							// ACTUALIZAR HP DO DEFENSOR
							if ($atacante->getArma()->getValue('nonLethalDamage'))
								$defensor->setValue("nonLethalDamage", $defensor->getValue("nonLethalDamage") + $dano_final);
							else $defensor->setValue("hp", $defensor->getValue("hp") - $dano_final);
						}
						
						/*
						echo '<br />';
						echo '<p>'.$jog1_initiative .'<>'. $jog2_initiative;
						echo '<br />round='.$_SESSION['rounds'] .'('.$atacante->getValue('nome').')';
						echo '<br />attack='.$attack_roll . '+' . $atacante->getValue('baseAttack') .'+'. $att_str_mod .'+'. $att_size_mod;
						echo '<br />armor= 10+'.$defensor->getValue('naturalArmor') . '+' . $def_dex_mod .'+'. $def_size_mod;
						echo '<br />acerta='.$resultado;
						echo '<p>damage_roll='. $damage_roll . ' dano final='. $dano_final . ', defensor hp='. $defensor->getValue("hp") . '</p>';
						die();
						*/
					} else {
						echo '<br />falhou!';
					}

					if ($_SESSION['ultimo_turno'] == 1) {
						$_SESSION['jog1'] = $atacante; $_SESSION['jog2'] = $defensor; $_SESSION['ultimo_turno'] = 2;
					} else {
						$_SESSION['jog2'] = $atacante; $_SESSION['jog1'] = $defensor; $_SESSION['ultimo_turno'] = 1;
					}
					
					if ($_SESSION['jog1']->getValue("hp") <= -10
						|| $_SESSION['jog1']->getValue("nonLethalDamage") > $_SESSION['jog1']->getValue("hp")) {
						$jog2_vit++; $continue = false;
					}
					if ($_SESSION['jog2']->getValue("hp") <= -10
						|| $_SESSION['jog2']->getValue("nonLethalDamage") > $_SESSION['jog2']->getValue("hp")) {
						$jog1_vit++;  $continue = false;
					}
				}
			}
			echo $jog2_vit . ' / '. $jog1_vit;
		}
		echo '</td>';
	}
	echo '</tr>';
}
?>
